{% extends "tribes/base.html" %}

{% load zwitschern %}
{% load i18n %}
{% load wiki %}
{% load wikiurl %}
{% load avatar_tags %}
{% load threadedcommentstags %}
{% load pagination_tags %}
{% load extra_tagging_tags %}
{% load tribe_tags %}
{% load scheduletags %}

{% block head_title %}{{ tribe.name }}{% endblock %}

{% block body %}

    {% if user.is_authenticated %}
        <div class="right_panel">
            <div class="members">
                <h2>{% trans "Members" %}</h2>
                <table width="100%">
                    {% for member in tribe.members.all %}
                        {% if forloop.counter0|divisibleby:"3" %}<tr>{% endif %}
                        <td>
                            <div class="avatar">{% avatar member 40 %}</div>
                            <div class="details"><a href="{% url profiles.views.profile member.username %}" title="{{ member.username }}">{{ member.username }}</a></div>
                        </td>
                        {% if forloop.counter0|add:"1"|divisibleby:"3" %}</tr>{% endif %}
                    {% endfor %}
                    {% if tribe.members.all|length|divisibleby:"3" %}{% else %}</tr>{% endif %}
                </table>
            </div>
        </div>
    {% endif %}

    <div style="width: 550px;">

        <h1>{% trans "Tribe" %} {{ tribe.name }}</h1>

        <p>
            {% trans "Slug:" %} <tt>{{ tribe.slug }}</tt><br />
            {% trans "Creator:" %} <a href="{% url profiles.views.profile tribe.creator.username %}">{{ tribe.creator }}</a><br />
            {% trans "Created:" %} {{ tribe.created|date }}
            <br />
            {% show_tags_for tribe %}
        </p>

        <p>{{ tribe.description }}</p>

        {% ifequal user tribe.creator %}
            <div class="form-toggle">
                <p><span id="edit-tribe-toggle">{% trans "Edit details" %}</span></p>
                
                <form id="edit-tribe" method="POST" action="">
                    <table>
                        {{ tribe_form }}
                        <tr><td></td><td><input type="hidden" name="action" value="update" /><input type="submit" value="{% trans 'update' %}"/></td></tr>
                    </table>
                </form>
            </div>
            
            {% ifequal tribe.members.all.count 1 %}
                <p><img src="/site_media/delete.png" /> <a href="#" onclick="$('#delete_tribe_form').toggle(); return false;">{% trans "Delete tribe" %}</a><p>
                 <form class="delete_form" id="delete_tribe_form" action="{% url tribe_delete tribe.slug %}" method="POST" style="display: none;">
                     <input type="submit" value="{% trans "Delete Tribe" %}" /> (all content will be removed)
                </form>
            {% else %}
                <p>You are not the only member of the tribe so you can't delete the tribe.</p>
            {% endifequal %}
            
        {% endifequal %}

        {% if user.is_authenticated %}
            <div>
                <form method="POST" action="">
                    {% if are_member %}
                        {% ifequal user tribe.creator %}
                            <p>{% trans "You are the creator of this tribe so can't leave (yet)." %}</p>
                        {% else %}
                            <input type="hidden" name="action" value="leave" />
                            <input type="submit" value="{% trans 'leave' %}"/>
                        {% endifequal %}
                    {% else %}
                        <input type="hidden" name="action" value="join" />
                        <input type="submit" value="{% trans "join tribe" %}"/>
                    {% endif %}
                </form>
            </div>
        {% else %}
            {% url acct_signup as signup_url %}
            {% url acct_login as login_url %}
            <p>{% blocktrans %}<a href="{{ signup_url }}">Sign up</a> and <a href="{{ login_url }}">log in </a> to join this tribe.{% endblocktrans %}</p>
        {% endif %}


        {% if user.is_authenticated %}
            <h2>{% trans "Discussion Topics" %}{% if tribe.topics.all.count %} ({{ tribe.topics.all.count }}){% endif %}</h2>

            {% if topics %}
                {% for topic in topics %}
                    {% show_tribe_topic topic %}
                {% endfor %}
                <p><a href="{% url tribe_topics tribe.slug %}">{% trans "more..." %}</a></p>
            {% else %}
                {% url tribe_topics tribe.slug as new_tribe_url %}
                <p>{% blocktrans %}None yet. <a href="{{ new_tribe_url }}">Start one...</a>{% endblocktrans %}</p>
            {% endif %}


            <h2>{% trans "Wiki Articles" %}{% if total_articles %} ({{ total_articles }}){% endif %}</h2>

            {% if articles %}
                <table class="topics">
                    {% for article in articles %}
                        {% show_teaser article %}
                    {% endfor %}
                </table>
                <p><a href="{% wikiurl list tribe %}">{% trans "more..." %}</a></p>
            {% else %}
                {% wikiurl list tribe as wiki_url %}
                <p>
                    {% blocktrans %}
                        None yet. Go to the <a href="{{ wiki_url }}" title="wiki">wiki</a> to start a new article.
                    {% endblocktrans %}
                </p>
            {% endif %}


            <h2>{% trans "Tweets" %}</h2>

            {% tweet_listing tweets 1 0 %}
            
            <h2>{% trans "Upcoming Events" %}</h2>
            {% get_calendar_for_object tribe as calendar %}
            {% if calendar.get_upcoming_events %}
                <table class="topics">
                    {% for event in calendar.get_upcoming_events %}
                        <tr class="odd">
                            <td>{{ event.title }}</td>
                            <td>{{ event.start }}-{{ event.end }}</td>
                        </tr>
                    {% endfor %}
                </table>
            {% else %}
                There are no upcoming events.
            {% endif %}
            <p>
                <a href="{{ calendar.get_absolute_url }}">calendar...</a>
            </p>


            {% if photos %}
                <h2>Tribe Photo Pool</h2>
                <div class="thumb-row clearfix">
                    {% for photo in photos %}
                    <div class="gallery-photo-thumb">
                        <a href="/photos/details/{{ photo.id }}/"><img src="{{ photo.get_thumbnail_url }}" alt="{{ photo.title }}"/></a><br />
                        <img src="/site_media/comment.png" border="0" class="noborder" align="absmiddle" /><a href="/photos/details/{{ photo.id }}/">{% get_comment_count for photo as commentcount %} {{ commentcount }} Comments</a>
                   </div>
                   {% endfor %}
               </div>
            {% endif %}


        {% endif %}
    </div>

{% endblock %}

{% block extra_body %}
    <script type="text/javascript">
        $(document).ready(function() {
            $('#edit-tribe').hide();
            $('#edit-tribe-toggle').click(function() {
                $('#edit-tribe').slideToggle();
                $('#edit-tribe').autoscroll();
                return false;
            });
            if ($('#edit-tribe ul.errorlist').length) {
                $('#edit-tribe').show();
                $('#edit-tribe ul.errorlist').autoscroll();
            }
        });
    </script>
    
{% endblock %}
